import { getDicts, getAllDicts } from '@/api/system/dict/data'

const dicts = {
  state: {
    //单位
    sys_unit: [],
    sys_unit_all: [],
    rd_technology: [],
  },

  mutations: {
    SET_DICT: (state, val) => {
      // console.log(val)
      state[val.key] = val.value
    },
    SET_DICT_All: (state, val) => {
      state[val.key + '_all'] = val.value
    },
  },

  actions: {
    /**
     * 获取有效的数据字典
     * @param {*} param0
     * @returns
     */
    GetDict({ commit, state }, key) {
      return new Promise((resolve, reject) => {
        getDicts(key)
          .then((res) => {
            commit('SET_DICT', {
              key: key,
              value: res.data,
            })
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    /**
     * 获取全部的数据字典
     * @param {*} param0
     * @returns
     */
    GetDictAll({ commit, state }, key) {
      return new Promise((resolve, reject) => {
        getAllDicts(key)
          .then((res) => {
            commit('SET_DICT_ALL', {
              key: key,
              value: res.data,
            })
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
  },
}

export default dicts
